package com.cgbl.cg.vm.resource

import android.app.Application
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.NumberUtils
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.PrefsManager
import com.robot.base.model.ResourceDetailBean
import com.robot.base.model.UserInfoBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.http.BaseResponse

class ResourceInfoViewModel(application: Application) : GLBaseViewModel(application) {
    val userAvatar = ObservableField<String>()
    val name = ObservableField<String>()
    val teacherFansField = ObservableField<String>("粉丝 ")
    val teacherFollowField = ObservableField<String>("关注 ")

    val naturalName = ObservableField<String>()
    val naturalNumber = ObservableField<String>("资源编号：")
    val naturalPrice = ObservableField<String>()
    val rulingPrice = ObservableField<String>("原价")
    val classVipPriceText = ObservableField<String>()
    val classVipPriceText1 = ObservableField<String>()
    val classVipPrice = ObservableField<String>()
    val payPromptText = ObservableField("")
    val introduce = ObservableField<String>()
    val userSign = ObservableField<String>()
    val follow = ObservableField(false)
    val followText = ObservableField<String>("+关注")
    var followerAccountId = ""

    val followCommand = BindingCommand<Any>(BindingAction {
        followUsers()
    })

    fun setData(bean: ResourceDetailBean) {
        naturalName.set(bean.naturalName)
        followerAccountId = bean.accountId ?: ""
        naturalNumber.set("资源编号：${bean.naturalNumber}")
        naturalPrice.set("¥${bean.naturalPrice}")
        rulingPrice.set("原价${bean.rulingPrice}")
        if (PrefsManager.getUserInfo().vipType == "1") {
            classVipPriceText.set("你已是超级VIP，购买教程")
            payPromptText.set("立即购买")
        } else {
            classVipPriceText.set("购买本教程+月VIP，")
            payPromptText.set("立即开通")
        }
        classVipPrice.set(bean.classVipPrice)
        classVipPriceText1.set("立省￥${((bean.naturalPrice?.toDouble() ?: 0.0) - (bean.classVipPrice?.toDouble() ?: 0.0))}")
        introduce.set(bean.introduce)
        fetchTeacherInfo(bean.accountId ?: "")
    }

    private fun fetchTeacherInfo(id: String) {
        UserServiceFactory.otherUserInfo(id)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<UserInfoBean>>(this) {
                override fun onNext(t: BaseResponse<UserInfoBean>) {
                    userAvatar.set(t.data.head)
                    name.set(t.data.nickName)
                    follow.set(t.data.follow)
                    followText.set(
                        if (follow.get() == true) {
                            "已关注"
                        } else {
                            "+关注"
                        }
                    )
                    teacherFansField.set("粉丝 ${t.data.fansCount}")
                    teacherFollowField.set("关注 ${t.data.followCount}")
                    userSign.set(t.data.userSign ?: "")
                }
            })
    }

    fun followUsers() {
        UserServiceFactory.followUsers(
            mapOf(
                "followerAccountId" to followerAccountId
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
            override fun onNext(t: BaseResponse<String>) {
                LogUtils.e(t)
                follow.set(!(follow.get() ?: false))
                followText.set(
                    if (follow.get() == true) {
                        "已关注"
                    } else {
                        "+关注"
                    }
                )
            }
        })
    }
}